package com.qst.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.domain.VipExpensesRecord;
import com.qst.domain.VipPointsRules;
import com.qst.domain.VipUser;
import com.qst.dto.Result;
import com.qst.dto.UserDTO;
import com.qst.mapper.VipPointsRulesMapper;
import com.qst.service.IVipPointsRulesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.MonthDay;
import java.util.Date;

@Service
public class IVipPointsRulesServiceImpl extends ServiceImpl<VipPointsRulesMapper, VipPointsRules> implements IVipPointsRulesService {

    @Resource
    private RestTemplate restTemplate;

    @Override
    public Result caluPoints(VipExpensesRecord vipExpensesRecord) {

        Long vipId = vipExpensesRecord.getVipId();
        String url = "http://vip-service/user/" + vipId;
        VipUser vipUser = restTemplate.getForObject(url, VipUser.class);
        if (vipUser == null){
            return Result.fail("获取会员用户错误");
        }
        Date vipBirthday = vipUser.getVipBirthday();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String birthday_time=sdf.format(vipBirthday);
        String[] strBirthday= birthday_time.toString().split("-");
        LocalDate myBir = LocalDate.of(Integer.parseInt(strBirthday[0]), Integer.parseInt(strBirthday[1]),Integer.parseInt(strBirthday[2]));
        LocalDate now = LocalDate.now();
        MonthDay monthDay = MonthDay.from(myBir);
        MonthDay nowMonthDay = MonthDay.from(now);
        boolean isBirthday = monthDay.equals(nowMonthDay);
        if (isBirthday){
            Integer arg = getRulesArgById(2L);
            postUserDTO(vipUser,vipExpensesRecord,arg);
            return Result.fail("更新积分余额成功");
        }
        Integer arg = getRulesArgById(1L);
        postUserDTO(vipUser,vipExpensesRecord,arg);
        return Result.fail("更新积分余额成功");
    }

    public Integer getRulesArgById(Long id) {
        VipPointsRules rule = query().eq("vip_rules_id",id).one();
        if (rule == null) {
            return 0;
        }
        return rule.getVipRuleArg();
    }

    @Override
    @Transactional
    public Result saveRule(VipPointsRules vipPointsRules) {
        boolean save = save(vipPointsRules);
        if (!save) {
            return Result.fail("新增积分规则失败");
        }
        return Result.ok("新增积分规则成功");
    }

    @Override
    public Result deleteRule(Long id) {
        boolean isSuccess = remove(new QueryWrapper<VipPointsRules>()
                .eq("vip_rules_id", id));
        if (!isSuccess) {
            return Result.fail("积分规则删除失败！");
        }
        return Result.ok("积分规则删除失成功！");
    }

    @Override
    public Result getRuleById(Long id) {
        VipPointsRules rule = query().eq("vip_rules_id", id).one();
        if (rule == null) {
            return Result.fail("此积分规则不存在");
        }
        return Result.ok(rule);
    }

    @Override
    @Transactional
    public Result updateRule(VipPointsRules vipPointsRules) {
        Long id = vipPointsRules.getVipRulesId();
        if (id == null) {
            return Result.fail("积分规则id不能为空！");
        }
        // 1.更新数据库
        boolean isSuccess = update(vipPointsRules, new QueryWrapper<VipPointsRules>().eq("vip_rules_id", id));
        if (!isSuccess) {
            return Result.ok("更新失败！");
        }
        return Result.ok("更新成功！");
    }

    public void postUserDTO(VipUser vipUser, VipExpensesRecord vipExpensesRecord, Integer arg){
        Long vipId = vipExpensesRecord.getVipId();
        float payment = vipExpensesRecord.getPayment();
        float newPoint = 0;
        newPoint = payment * arg;
        String point = vipUser.getPoint();
        float nowPoints = Float.parseFloat(point) + newPoint;
        UserDTO userDTO = new UserDTO();
        userDTO.setVipId(vipId);
        userDTO.setPoint(nowPoints);
        String url = "http://vip-service/user/update/userDto";
        restTemplate.postForObject(url, userDTO, String.class);
    }

}
